using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DTO;
using DAL;
using BUS;
using Utility;
public partial class SanPham_Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }
        if (Session["UserName"] == null)
        {
            HyperLink1.Visible = false;
            LinkButton1.Visible = false;
        }
        else
        {
            HyperLink1.Text = Session["UserName"].ToString();
            LinkButton1.Visible = true;
            HyperLink1.Visible = true;
        }
            
    }
    private void LoadData()
    {
        PagerV2_8_1.PageSize = 10;
        Criteria c = new Criteria();
        
        int total = 0;
        c.add(Condition.AND, tbSanPhamColumns .tinhtrang , Condition.NOT_EQUAL, 12);
        c.add(Condition.AND,tbSanPhamColumns .tinhtrang , Condition.NOT_EQUAL, 30);
        tbSanPhamCollection sanphamCollection = tbSanPhamBUS.Get(c.Criter, PagerV2_8_1.CurrentIndex, PagerV2_8_1.PageSize, tbSanPhamColumns.masp.ToString(), OrderDirection.ASC.ToString(), out total);
        Repeater1.DataSource = sanphamCollection;
        Repeater1.DataBind();
        PagerV2_8_1.ItemCount = total;
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
         e.Item.ItemType == ListItemType.Item)
        {
            tbSanPham sp = (tbSanPham)e.Item.DataItem;
            //if (sp.tinhtrang == 20)
            //{
            Literal lit_tietkiem = (Literal)e.Item.FindControl("lit_tietkiem");
            Literal lit_ngaysudung = (Literal)e.Item.FindControl("lit_ngaysudung");
            Literal lit_damua = (Literal)e.Item.FindControl("lit_damua");
            Literal lit_tinvantat = (Literal)e.Item.FindControl("lit_tinvantat");
            Image img_hinhanh = (Image)e.Item.FindControl("img_HinhAnh");

            HyperLink hpl_tensp = (HyperLink)e.Item.FindControl("hpl_tensp");

            HyperLink hyp_xemchitiet = (HyperLink)e.Item.FindControl("hyp_xemchitiet");
            hyp_xemchitiet.NavigateUrl = "~/SanPham/chitietsanpham.aspx?id=" + sp.masp.ToString();
            HyperLink hyp_xem = (HyperLink)e.Item.FindControl("hyp_mua");
            hyp_xem.NavigateUrl = "~/SanPham/giohang.aspx?id=" + sp.masp.ToString();
            //Image img_tinhtrang = (Image)e.Item.FindControl("img_tinhtrang");


            Label lb_tinhtrang = (Label)e.Item.FindControl("lb_tinhtrang");
            Image img_tinhtrang =(Image )e.Item .FindControl ("img_tinhtrang");
            tbTinhTrang ttrang = tbTinhTrangBUS.GetByID(sp.tinhtrang);
            hpl_tensp.Text = sp.tensp.ToString();
            hpl_tensp.NavigateUrl = "~/SanPham/chitietsanpham.aspx?id=" + sp.masp.ToString();
            if (ttrang.matinhtrang == 11 || ttrang.matinhtrang == 20)
            {
                if (ttrang.matinhtrang == 20)
                {
                    img_tinhtrang.ImageUrl = "~/SanPham/images/chayhang.png";
                }
                else
                {

                    if (ttrang.matinhtrang == 11)
                    {
                        img_tinhtrang.ImageUrl = "~/SanPham/images/hethang.png";
                    }
                }
            }
            else
            {
                img_tinhtrang.Visible = false;
            }         

            double tietkiem = (sp.giaban) * 100 / (sp.giagoc);
            lit_damua.Text = sp.damua.ToString();
            lit_tietkiem.Text = string.Format("{0:0}", tietkiem);
            lit_tinvantat.Text = sp.tinvantat .ToString();

            //DateTime now = DateTime.Now;
            //DateTime enddate = sp.ngayhethan;

            //TimeSpan t = enddate - now;
            //sogiay = double.Parse(t.TotalSeconds.ToString());
            DateTime hethan = sp.ngayhethan;
            DateTime sudung = sp.ngaysudung;
            TimeSpan t = hethan - sudung;
            //DateTime ngay = (sp.ngayhethan) - (sp.ngaysudung);
            //hyp_tensp.NavigateUrl = "~/SanPham/SanPham_Detail.aspx?id=" + sp.masp.ToString();
            img_hinhanh.ImageUrl = "~/Photos/" + sp.hinhchinh;
            //}

        }
    }
    protected void PagerV2_8_1_Command(object sender, CommandEventArgs e)
    {
        PagerV2_8_1.CurrentIndex = int.Parse(e.CommandArgument.ToString());
        LoadData();
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Session["UserName"] = null;
        HyperLink1.Visible = false;
        LinkButton1.Visible = false;
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        Session["UserName"] = null;
        Response.Redirect("~/SanPham/D_NHAP.aspx");
    }
}
